<template>
	<el-container>
		<el-main class="nopadding">
			<el-container>
				<el-header>
					<div class="left-panel">
						<el-button circle icon="el-icon-edit-pen" title="发送" @click="sendEmail"></el-button>
						<el-button circle icon="el-icon-close" title="删除" :disabled="selection.length==0" @click="batch_del"></el-button>
					</div>
					<div class="right-panel">
						<div class="right-panel-search">
							<el-input v-model="search.keyword" placeholder="检索邮件" clearable></el-input>
							<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						</div>
					</div>
				</el-header>
				<el-main class="nopadding">
					<scTable ref="table" :apiObj="apiObj" row-key="emailId" @selection-change="selectionChange" stripe remoteSort remoteFilter highlightCurrentRow >
						<el-table-column type="selection" width="50"></el-table-column>
						<el-table-column label="邮件标题" prop="subject" width="300" :show-overflow-tooltip="true"></el-table-column>
						<el-table-column label="接收人" prop="toUserName" width="200" :show-overflow-tooltip="true"></el-table-column>
						<el-table-column label="邮件内容" prop="subheading" width="500" :show-overflow-tooltip="true"></el-table-column>
						<el-table-column label="附件" prop="attach" width="300">
							<template #default="scope">
								<attach-menu :attachIds="scope.row.attachId" :roleFlag="1" :preview="true"></attach-menu>
							</template>
						</el-table-column>
						<el-table-column label="发送时间" prop="sendTime" width="150"></el-table-column>
						<el-table-column label="操作" fixed="right" align="right" width="220">
							<template #default="scope">
								<el-button-group>
									<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
									<el-button text type="primary" size="small" @click="reSendEmail(scope.row, scope.$index)">重发</el-button>
									<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
										<template #reference>
											<el-button text type="primary" size="small">删除</el-button>
										</template>
									</el-popconfirm>
								</el-button-group>
							</template>
						</el-table-column>
					</scTable>
				</el-main>
			</el-container>
		</el-main>
	</el-container>
	<email-details v-if="dialog.detailsSave" ref="emailsDialog" @closed="dialog.detailsSave=false"></email-details>
	<send-email-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></send-email-dialog>
</template>
<script>
import sendEmailDialog from './sendEmailDialog'
import AttachMenu from '@/views/module/attachMenu'
import emailDetails from "./emailDetails";
export default {
	name: 'Email Send Box',
	components: {
		emailDetails,
		AttachMenu,
		sendEmailDialog
	},
	data() {
		return {
			apiObj:this.$API.email.getDraftBoxEmail,
			dialog: {
				detailsSave:false,
				save: false
			},
			selection: [],
			search: {
				keyword: ""
			}
		}
	},
	mounted() {
	},
	methods:{
		selectionChange(selection) {
			this.selection = selection;
		},
		sendEmail(){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open("send")
			})
		},
		table_show(row,index){
			this.dialog.detailsSave = true
			this.$nextTick(() => {
				this.$refs.emailsDialog.open().setData(row);
			})
		},
		async table_del(row,index){
			var reqData = {bodyId: row.bodyId}
			var res = await this.$API.email.deleteMySendEmail.post(reqData);
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async batch_del() {
			await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				var bodyIdArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					bodyIdArr.push(this.selection[i].bodyId);
				}
				const loading = this.$loading();
				var res = await this.$API.email.deleteMySendEmailByIds.post({bodyIds: bodyIdArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$emit('changefoldersuccess')
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		reSendEmail(row,index)
		{
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open("send").setData(row)
			})
		},
		handleSaveSuccess() {
			this.$refs.table.refresh()
		},
		upsearch()
		{
			this.$refs.table.upData(this.search)
		}
	}
}
</script>
